package com.yhd.service.impl;

import com.yhd.entity.TCourse;
import com.yhd.entity.TCycleImg;
import com.yhd.entity.dto.CycleImgDto;
import com.yhd.entity.dto.UserDto;
import com.yhd.entity.query.CycleImgVO;
import com.yhd.mapper.CycleImgMapper;
import com.yhd.service.CycleImgService;
import com.yhd.utils.BeanMapperUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Condition;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Transactional(rollbackFor = Exception.class)
@Service
public class CycleImgServiceImpl implements CycleImgService {
	@Autowired
	private CycleImgMapper cycleImgMapper;
	
	/**
	 * 查询课程轮播图列表
	 * @return
	 * @param type
	 */
	@Override
	public List<CycleImgDto> getCycleList(String type) {
		Example example = new Example(TCycleImg.class);
		Example.Criteria criteria = example.createCriteria().andEqualTo("bizType", type).andEqualTo("status", 1);
		example.setOrderByClause("createTime desc");
		List<TCycleImg> cycleImgs = cycleImgMapper.selectByExample(example);
		List<CycleImgDto> result = new ArrayList<>();
		if (!CollectionUtils.isEmpty(cycleImgs)) {
			result = BeanMapperUtil.copyList(cycleImgs, CycleImgDto.class);
		}
//		result = result.stream().map(cycleImgDto -> {
//			if (Objects.equals(cycleImgDto.getType(),"img") && !StringUtils.isEmpty(cycleImgDto.getImg())){
//				cycleImgDto.setImg(Constant.DOMAIN + cycleImgDto.getImg());
//			}
//			return cycleImgDto;
//		}).collect(Collectors.toList());
		return result;
	}

	@Override
	public List<CycleImgDto> selectCycleImgList(CycleImgVO cycleImgVO) {
		Condition condition=new Condition(TCycleImg.class);
		condition.setOrderByClause("createTime desc");
		List<TCycleImg> cycleImgList = cycleImgMapper.selectByExample(condition);
		List<CycleImgDto> cycleImgDtoList = new ArrayList<>();
		if (null != cycleImgList && cycleImgList.size() > 0){
			for (TCycleImg tCycleImg:cycleImgList) {
				CycleImgDto cycleImgDto = new CycleImgDto();
				BeanUtils.copyProperties(tCycleImg,cycleImgDto);
				cycleImgDtoList.add(cycleImgDto);
			}
		}
		return cycleImgDtoList;
	}

	/**
	 * 获取banner详情
	 * @param cycleImgVO
	 * @return
	 */
	@Override
	public CycleImgDto selectCycleImgInfo(CycleImgVO cycleImgVO) {
		if (null != cycleImgVO && null != cycleImgVO.getId()){
			TCycleImg tCycleImg = new TCycleImg();
			tCycleImg.setId(cycleImgVO.getId());
			TCycleImg newCycleImg = cycleImgMapper.selectOne(tCycleImg);
			if (null != newCycleImg){
				CycleImgDto cycleImgDto = new CycleImgDto();
				BeanUtils.copyProperties(newCycleImg,cycleImgDto);
				return cycleImgDto;
			}
		}
		return null;
	}

	/**
	 * 删除banner
	 * @param cycleImgVO
	 * @return
	 */
	@Override
	public boolean deleteCycleImg(CycleImgVO cycleImgVO) {
		if (null != cycleImgVO && null != cycleImgVO.getId()) {
			TCycleImg tCycleImg = new TCycleImg();
			tCycleImg.setId(cycleImgVO.getId());
			int deleteCycleImg = cycleImgMapper.delete(tCycleImg);
			if (deleteCycleImg > 0) {
				return true;
			}
		}
		return false;
	}

	/**
	 * 新增、修改banner
	 * @param cycleImgVO
	 * @param userInfo
	 * @return
	 */
	@Override
	public CycleImgDto putBanner(CycleImgVO cycleImgVO, UserDto userInfo) {
		if (null != cycleImgVO){
			if (null != cycleImgVO.getId()){
				//修改
				TCycleImg tCycleImg = new TCycleImg();
				BeanUtils.copyProperties(cycleImgVO,tCycleImg);
				tCycleImg.setUpdateTime(new Date());
				tCycleImg.setUpdateUserId(userInfo.getId());
				int updateCycleImg = cycleImgMapper.updateByPrimaryKeySelective(tCycleImg);
				if (updateCycleImg > 0){
					CycleImgDto cycleImgDto = new CycleImgDto();
					BeanUtils.copyProperties(tCycleImg,cycleImgDto);
					return cycleImgDto;
				}
			}else {
				//新增
				TCycleImg tCycleImg = new TCycleImg();
				BeanUtils.copyProperties(cycleImgVO,tCycleImg);
				tCycleImg.setCreateTime(new Date());
				tCycleImg.setType("img");
				tCycleImg.setCreateUserId(userInfo.getId());
				tCycleImg.setUpdateTime(new Date());
				tCycleImg.setUpdateUserId(userInfo.getId());
				int insertCycleImg = cycleImgMapper.insert(tCycleImg);
				if (insertCycleImg > 0){
					CycleImgDto cycleImgDto = new CycleImgDto();
					BeanUtils.copyProperties(tCycleImg,cycleImgDto);
					return cycleImgDto;
				}
			}
		}
		return null;
	}
}
